﻿package com.tangchao.client.sdh.view.role
{
	import com.tangchao.client.entities.User;
	import com.tangchao.client.sdh.definition.DFRolePlace;
	import com.tangchao.client.sdh.definition.DFState;
	import com.tangchao.client.sdh.definition.DFSuoFangDaXiao;
	import com.tangchao.client.sdh.event.ClickRoleEvent;
	import com.tangchao.client.sdh.vo.Player;
	import com.tangchao.client.sdh.vo.RoleInfoVo;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	public class BaseRole extends MovieClip
	{
		private var _bk:MovieClip;
		private var _name:TextField;
		private var _clock:Clock;
		private var _tips:QiPao;
		private var _place:int=DFRolePlace.PLACE_LEFT;
		private var _daoGenFanStat:MovieClip;
		private var _mcDizhu:MovieClip;
		private var _mcTuoguan:MovieClip;
		
		private var _player:Player;
		
		private var _stat:int = DFState.NONE_STAT;
		private var _isTuoguan:Boolean = false;
		private var _isZJ:Boolean = false;
		
		public function BaseRole()
		{
			stop();
			init();
//			this.x = this.x+516; //516 384为面板背景的坐标  416.95 132.65
//			this.y = this.y+384;
			addEventListener(MouseEvent.CLICK,onMouseClick);
		}
		
		private function onMouseClick(e:MouseEvent)
		{
			var evt:ClickRoleEvent=new ClickRoleEvent();
			evt.place=_place;
			evt.vo=_player;
			dispatchEvent(evt);
		}
		
		private function init()
		{
			_bk=getChildByName("mcRoleBk") as MovieClip;
			_name=getChildByName("tfName") as TextField;
			_clock=getChildByName("mcClock") as Clock;
			_tips=getChildByName("mcReady") as QiPao;
			_mcDizhu = getChildByName("mcDizhu") as MovieClip;
			_mcDizhu.visible = false;
			_mcTuoguan = getChildByName("mcTuoguan") as MovieClip;
			_mcTuoguan.visible = false;
			
			_daoGenFanStat = getChildByName("mcDaoGenFan") as MovieClip;
			_daoGenFanStat.stop();
			_daoGenFanStat.visible = false;
		}
		
		public function set place(value:int)
		{
			_place=value;
		}
		
		public function create(player:Player, isSetViewPos:Boolean=false)
		{
			var v:User;
			if(_player && !isSetViewPos)
			{
				_player.ui = null;
				//setStat(DFState.READY_NO);
			}
			
			if(player)
			{
				_player = player;
				v = player.user;
				_name.text=v.getName();
				_bk.visible = true;
				if(v.getGender() == 0)
				{
					_bk.gotoAndStop(1);
				}
				else
				{
					_bk.gotoAndStop(2);
				}
			}
			else
			{
				destory();
			}
		}
		
		public function destory():void
		{
			_bk.visible = false;
			_name.text = "";
			setStat(DFState.NONE_STAT);
			_mcDizhu.visible = false;
			_mcTuoguan.visible = false;
			noTime();
		}
		
		private function restoreStat():void
		{
			create(_player, true);
			setStat(_stat);
			doTuoguan(_isTuoguan);
			setZJ(_isZJ);
		}
		
		public function setStat(stat:int):void
		{
			_stat = stat;
			switch(_stat)
			{
				case DFState.READY_NO:
					_daoGenFanStat.visible = false;
					break;
				case DFState.READY_YES:
					_daoGenFanStat.visible = false;
					_tips.setStat(QiPao.YIZHUNBEI);
					break;
				case DFState.STAT_DAO:
					_daoGenFanStat.visible = true;
					_daoGenFanStat.gotoAndStop(1);
					break;
				case DFState.STAT_GEN:
					_daoGenFanStat.visible = true;
					_daoGenFanStat.gotoAndStop(2);
					break;
				case DFState.STAT_FAN:
					_daoGenFanStat.visible = true;
					_daoGenFanStat.gotoAndStop(3);
					break;
				default:
					_daoGenFanStat.visible = false;
					break;
			}
		}
		
		public function doBet(val:int):void
		{
			noTime();
			if(val > 0)
			{
				_tips.setText(val.toString());
			}
			else
			{
				_tips.setStat(QiPao.BUJIAO);
			}
		}
		
		public function doTuoguan(isTuoguan:Boolean):void
		{
			_isTuoguan = isTuoguan;
			_mcTuoguan.visible = isTuoguan;
		}
		
		public function setZJ(val:Boolean):void
		{
			_isZJ = val;
			_mcDizhu.visible = val;
		}
		
		public function setTimer(value:int, func:Function):void
		{
			_clock.setTimer(value, func);
		}
		
		public function noTime()
		{
			_clock.stopTimer();
		}
		
		public function setViewPos(viewPos:int):void
		{
			switch(viewPos)
			{
				case DFSuoFangDaXiao.JIN:
						gotoAndStop(2);
						init();
						setItemsPos(getJinPos());
						restoreStat();
						break;
				case DFSuoFangDaXiao.YUAN:
					gotoAndStop(3);
					init();
					setItemsPos(getYuanPos());
					restoreStat();
					break;
				case DFSuoFangDaXiao.ZHONG:
					gotoAndStop(1);
					init();
					setItemsPos(getZhongPos());
					restoreStat();
					break;
				default:
					break;
			}
		}
		
		private function setItemsPos(posArr:Array):void
		{
			var pos:Point;
			if(posArr)
			{
				pos = posArr[0] as Point;
				x = pos.x;
				y = pos.y;
				
				pos = posArr[1] as Point;
				_bk.x = pos.x-parent.x+516;
				_bk.y = pos.y-parent.y+384;
				
				pos = posArr[2] as Point;
				_name.x = pos.x-parent.x+516;
				_name.y = pos.y-parent.y+384;
				
				
				pos = posArr[3] as Point;
				_clock.x = pos.x-parent.x+516;
				_clock.y = pos.y-parent.y+384;
				
				pos = posArr[4] as Point;
				_mcTuoguan.x = pos.x-parent.x+516;
				_mcTuoguan.y = pos.y-parent.y+384;
				
				pos = posArr[5] as Point;
				_daoGenFanStat.x = pos.x-parent.x+516;
				_daoGenFanStat.y = pos.y-parent.y+384;
				
				pos = posArr[6] as Point;
				_tips.x = pos.x-parent.x+516;
				_tips.y = pos.y-parent.y+384;
				
				pos = posArr[6] as Point;
				_mcDizhu.x = pos.x-parent.x+516;
				_mcDizhu.y = pos.y-parent.y+384;
				
			}
		}
		
		public function getJinPos():Array
		{
			return null;
		}
		
		public function getZhongPos():Array
		{
			return null;
		}
		
		public function getYuanPos():Array
		{
			return null;
		}
	}
}